package net.dl.ss.service;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class ContextManager {
	public final static String CURRENT_EVENT = "current_event";
	public final static String CURRENT_CATEGORY = "current_category";
	
	protected static final Log logger = LogFactory.getLog(ContextManager.class);
	
	
	public static void setCurrentEvent(HttpServletRequest request) {
		String current_event = request.getParameter(CURRENT_EVENT);
		if(current_event!=null && !"".equals(current_event)) {
			Long new_event= Long.valueOf(current_event);
			//Long old_event = (Long)request.getSession().getAttribute(CURRENT_EVENT);
			//if(old_event==null || !new_event.equals(old_event)) {
				request.getSession().setAttribute(CURRENT_EVENT, new_event);
				logger.info("event:"+new_event);
				setCurrentCategory(request, null);
			//}
		}
	}
	
	public static void setCurrentCategory(HttpServletRequest request) {
		String current_category = request.getParameter(CURRENT_CATEGORY);
		if(current_category!=null && !"".equals(current_category)) {
			 request.getSession().setAttribute(CURRENT_CATEGORY, Long.valueOf(current_category));
			 logger.info("category:"+current_category);
		}
	}
	
	public static Long getCurrentEvent(HttpServletRequest req) {
		return (Long)req.getSession().getAttribute(CURRENT_EVENT);
	}

	public static Long getCurrentCategory(HttpServletRequest req) {
		return (Long)req.getSession().getAttribute(CURRENT_CATEGORY);
	}

	private static void setCurrentCategory(HttpServletRequest req, String value) {
		req.getSession().setAttribute(CURRENT_CATEGORY, value);
		logger.info("category:"+value);
	}

}
